package com.chenqq.qsocket.message;

import org.json.JSONException;
import org.json.JSONObject;

public class TextMessage extends SocketMessage{
    public TextMessage(SocketMessage msg) {
        super(msg);
    }

    public TextMessage(long id,byte type,String sender,String receiver,String des) throws JSONException {
        setType(type);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        jsonObject.put("sender",sender);
        jsonObject.put("receiver",receiver);
        jsonObject.put("content", des);
        jsonObject.put("hex",type==SocketMessage.TYPE_SEND_TEXT_HEX);
        setBody(jsonObject.toString());
    }

    public MessageInfo getMessageInfo() throws JSONException {
        JSONObject json = new JSONObject(getJSONBody());
        MessageInfo resultInfo = new MessageInfo();
        resultInfo.id = json.getLong("id");
        resultInfo.sender=json.getString("sender");
        resultInfo.receiver=json.getString("receiver");
        resultInfo.content=json.getString("content");
        resultInfo.isHex=json.getBoolean("hex");
        return resultInfo;
    }

    public static class MessageInfo {
        public long id;
        public String content;
        public String sender;
        public String receiver;
        public boolean isHex;
    }
}
